
load nxnz.0
nx=nxnz(1);
ny=nxnz(2);
%nx=200;
%ny=36;

nodes = 1;

fid_t = fopen('time.0','r',plat);
%fid_visc = fopen('visc.0','r',plat);
fid_mesh = fopen('mesh.0','r',plat);
fid_heat = fopen('hfl.0','r',plat);

if nodes == 1;
    fid_Nv = fopen('Nv.0','r',plat);
    fid_Nf = fopen('Nf.0','r',plat);
end

fid_vx = fopen('vx.0','r',plat);
fid_vz = fopen('vz.0','r',plat);

t = fread(fid_t,'single');
nt=length(t);

if exist('particles.0','file')>0
    fid_part = fopen('particles.0','r',plat);
    junk = fread(fid_part,'single'); 
    particles = reshape(junk,length(junk)/2/length(t),2,length(t));
end


if (exist('fid1') == 1)
    junk = fread(fid1,'single');  A1=reshape(junk,ny,nx,nt);
end
if (exist('fid2') == 1)
    junk = fread(fid2,'single');  A2=reshape(junk,ny,nx,nt);
end
if (exist('fid3') == 1)
    junk = fread(fid3,'single');  A3=reshape(junk,ny,nx,nt);
end
if (exist('fid4') == 1)
    junk = fread(fid4,'single');  A4=reshape(junk,ny,nx,nt);
end
if (exist('fid5') == 1)
    junk = fread(fid5,'single');  A5=reshape(junk,ny,nx,nt);
end
if (exist('fid6') == 1)
    junk = fread(fid6,'single');  A6=reshape(junk,ny,nx,nt);
end
junk = fread(fid_vx,'single');  vx=reshape(junk,ny,nx,nt);
junk = fread(fid_vz,'single');  vz=reshape(junk,ny,nx,nt);
junk = fread(fid_mesh,'single'); mesh=reshape(junk,(ny+1),(nx+1),2,nt);
meshxx=zeros(ny+1,nx+1,nt);
meshzz=meshxx;
meshxx(:)=mesh(:,:,1,:);
meshzz(:)=mesh(:,:,2,:);

junk = fread(fid_heat,'single'); heat = reshape (junk,nx+1,nt);

if nodes == 1
     junk = fread(fid_Nv,'single'); Nv=reshape(junk,(ny+1),(nx+1),2,nt);
     Nvx=zeros(ny+1,nx+1,nt);
     Nvz=Nvx;
     Nvx(:)=Nv(:,:,1,:);
     Nvz(:)=Nv(:,:,2,:);

     junk = fread(fid_Nf,'single'); Nf=reshape(junk,(ny+1),(nx+1),2,nt);
     Nfx=zeros(ny+1,nx+1,nt);
     Nfz=Nfx;
     Nfx(:)=Nf(:,:,1,:);
     Nfz(:)=Nf(:,:,2,:);
end

fclose(fid_t);
if (exist('fid1') == 1)
  fclose(fid1);
end
if (exist('fid2') == 1)
  fclose(fid2);
end
if (exist('fid3')==1)
  fclose(fid3);
end
if (exist('fid4')==1)
  fclose(fid4);
end
if (exist('fid5')==1)
  fclose(fid5);
end
if (exist('fid6')==1)
  fclose(fid6);
end
fclose(fid_mesh);
if nodes == 1
    fclose(fid_Nv);
    fclose(fid_Nf);
end
fclose(fid_vx);
fclose(fid_vz);
fclose(fid_heat);


%Sort Particles
if exist('particles.0','file')>0
    fclose(fid_part);
    for i = 1:length(t)
        [srt,psrt]=sort(particles(:,1,i),1,'ascend');
        topo(:,1,i)=particles(psrt,1,i);
        topo(:,2,i)=particles(psrt,2,i);
    end
end


Xcnr=zeros([ny*nx,4,nt]);Zcnr=Xcnr;
for k=1:nt;
for i = 1:ny
  Xcnr(nx*(i-1)+1:nx*i,:,k)=[meshxx(i,1:nx,k)', meshxx(i+1,1:nx,k)', meshxx(i+1,2:nx+1,k)', meshxx(i,2:nx+1,k)'];
 Zcnr(nx*(i-1)+1:nx*i,:,k)=[meshzz(i,1:nx,k)', meshzz(i+1,1:nx,k)', meshzz(i+1,2:nx+1,k)', meshzz(i,2:nx+1,k)'];
end;
end

Xcnt = meshxx(1:end-1,1:end-1,:) + ...
    (meshxx(2:end,2:end,:)-meshxx(1:end-1,1:end-1,:))/2;
Zcnt = meshzz(1:end-1,1:end-1,:) + ...
    (meshzz(2:end,2:end,:)-meshzz(1:end-1,1:end-1,:))/2;


if exist('tracer.0','file')>0
   trac = fopen('tracer.0','r',plat);
    junk = fread(trac,'single'); 
    tracer = reshape(junk,2,length(t))';
end